package com.tnstart.utils
	//package com.zqgame.llz.utils
{
	import flash.utils.Dictionary;
	
	/**
	 * 哈希表 
	 * @author jinchao.xiong
	 * 
	 */ 
	public class HashMap
	{
		private var _content:Dictionary;
		private var _len:int;
		
		public function HashMap(weakKeys:Boolean = false)
		{
			_content = new Dictionary(weakKeys);
			_len = 0;
		}
		
		public function get length():int
		{
			return _len
		}
		
		public function containKey(key:*):Boolean
		{
			if(key in _content)
			{
				return true;
			}
			return false;
		}
		
		public function containValue(value:*):Boolean
		{
			for(var key:* in _content)
			{
				if(_content[key] === value)
				{
					return true;
				}
			}
			return false;
		}
		
		public function add(key:*, value:*):void
		{
			if(!containKey(key))
			{
				_len++;
			}
			_content[key] = value;
		}
		
		public function remove(key:*):void
		{
			if(containKey(key))
			{
				_len--;
				delete _content[key];
			}
		}
		
		public function getKey(value:*):*
		{
			for(var key:* in _content)
			{
				if(_content[key] === value)
				{
					return key;
				}
			}
			return null;
		}
		
		public function getValue(key:*):*
		{
			if(key in _content)
			{
				return _content[key];
			}
			return null;
		}
		
		public function eachKey(func:Function):void
		{
			var i:*;
			for (i in _content) 
			{
				func(i);
			};
		}
		
		public function getKeys():Array
		{
			var arr:Array = [];
			for(var key:* in _content)
			{
				arr.push(key);
			}
			return arr;
		}
		
		public function getValues():Array
		{
			var arr:Array = [];
			for(var key:* in _content)
			{
				arr.push(_content[key]);
			}
			return arr;
		}
		
		public function clear():void
		{
			for(var key:* in _content)
			{
				delete _content[key];
			}
		}
		
		public  function addHash(data:Object,injectObj:*,key:String):void
		{
			if(data!=null )
			{
				add(data.key,injectObj);
			}
		}
		
		public  function arrayIntoHash(arr:Array,injectObj:*,key:String):void
		{
			for each(var obj:Object in arr)
			{
				addHash(obj,injectObj,key);
			}
		}
		
	}
}